/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Wx
 */
@Entity
public class QuotationItem implements Serializable, Comparable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private Double unitPrice;
    private Double subTotal;
    private Integer quantity;
    private Double discount;
    @OneToOne(cascade = {CascadeType.PERSIST}, fetch=FetchType.EAGER)
    private Product product;
    private Double shippingRate;
    @OneToOne(cascade = {CascadeType.PERSIST}, fetch=FetchType.EAGER)
    private Address shippingAddress;
    @Temporal(value = TemporalType.DATE)
    private Date requestDate;
    private String shippingMethod;

    public QuotationItem() {
    }

    public QuotationItem(Double unitPrice, Double subTotal, Integer quantity, Double discount, Product product, Double shippingRate, Address shippingAddress, Date requestDate, String shippingMethod) {
        this.setUnitPrice(unitPrice);
        this.setSubTotal(subTotal);
        this.setQuantity(quantity);
        this.setDiscount(discount);
        this.setProduct(product);
        this.setShippingRate(shippingRate);
        this.setShippingAddress(shippingAddress);
        this.setRequestDate(requestDate);
        this.setShippingMethod(shippingMethod);
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the unitPrice
     */
    public double getUnitPrice() {
        return unitPrice;
    }

    /**
     * @param unitPrice the unitPrice to set
     */
    public void setUnitPrice(Double unitPrice) {
        this.unitPrice = unitPrice;
    }

    /**
     * @return the subTotal
     */
    public Double getSubTotal() {
        return subTotal;
    }

    /**
     * @param subTotal the subTotal to set
     */
    public void setSubTotal(Double subTotal) {
        this.subTotal = subTotal;
    }

    /**
     * @return the quantity
     */
    public Integer getQuantity() {
        return quantity;
    }

    /**
     * @param quantity the quantity to set
     */
    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    /**
     * @return the discount
     */
    public Double getDiscount() {
        return discount;
    }

    /**
     * @param discount the discount to set
     */
    public void setDiscount(Double discount) {
        this.discount = discount;
    }

    /**
     * @return the product
     */
    public Product getProduct() {
        return product;
    }

    /**
     * @param product the product to set
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    /**
     * @return the shippingRate
     */
    public Double getShippingRate() {
        return shippingRate;
    }

    /**
     * @param shippingRate the shippingRate to set
     */
    public void setShippingRate(Double shippingRate) {
        this.shippingRate = shippingRate;
    }

    /**
     * @return the shippingAddress
     */
    public Address getShippingAddress() {
        return shippingAddress;
    }

    /**
     * @param shippingAddress the shippingAddress to set
     */
    public void setShippingAddress(Address shippingAddress) {
        this.shippingAddress = shippingAddress;
    }

    /**
     * @return the requestDate
     */
    public Date getRequestDate() {
        return requestDate;

    }

    /**
     * @param requestDate the requestDate to set
     */
    public void setRequestDate(Date requestDate) {
        this.requestDate = requestDate;
    }

    /**
     * @return the shippingMethod
     */
    public String getShippingMethod() {
        return shippingMethod;
    }

    /**
     * @param shippingMethod the shippingMethod to set
     */
    public void setShippingMethod(String shippingMethod) {
        this.shippingMethod = shippingMethod;
    }
    
    
     @Override
    public int compareTo(Object obj) {
        QuotationItem qi = (QuotationItem) obj;
        if (qi.product.getCode().compareTo(this.product.getCode()) < 0) {
            return 1;
        } else if (qi.product.getCode().compareTo(this.product.getCode()) > 0) {
            return -1;
        } else {
            if (qi.quantity < this.quantity) {
                return 1;
            } else if (qi.quantity > this.quantity) {
                return -1;
            } else {
                if (qi.unitPrice < this.unitPrice) {
                    return 1;
                } else if (qi.unitPrice > this.unitPrice) {
                    return -1;
                } else {
                    if (qi.discount < this.discount) {
                        return 1;
                    } else if (qi.discount > this.discount) {
                        return -1;
                    }
                    else
                    {
                        if (qi.shippingRate < this.shippingRate)
                            return 1;
                        else if (qi.shippingRate > this.shippingRate)
                            return -1;
                    }
                }

            }

        }

        return 0;
    }

     
}
